﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using UnityEngine;



/// <summary>
/// Mod源文件描述
/// </summary>
public partial class ModMetaData
{
    public class ModMetaFileData
    {
        public string packageId = "";

        public string name = "";

        public string author = "Anonymous";

        /// <summary>
        /// mod的自己版本号定义
        /// </summary>
        public string modVersion = "";
        public string url = "";
        public string description = "No description provided."; // 描述
        public List<string> supportedVersions = new List<string>(); // 支持的版本列表


        public List<string> LoadBefore = new List<string>();// 在...之前加载
        public List<string> LoadAfter = new List<string>(); // 在...之后加载
        public List<string> incompatibleWith = new List<string>(); // 与...不兼容

        private List<string> loadVersion = new List<string>();
        /// <summary>
        /// Mod依赖项
        /// </summary>
        //public List<ModDependency> modDependencies = new List<ModDependency>();

    }
    public ModMetaFileData meta = new ModMetaFileData();

    private DirectoryInfo rootDirInt;
    public DirectoryInfo RootDir => rootDirInt;

    public string SupportedVersions
    {
        get
        {
            StringBuilder stringBuilder = new StringBuilder();


            for (int i = 0; i < meta.supportedVersions.Count; i++)
            {
                stringBuilder.Append(meta.supportedVersions[i]);
                if (i != meta.supportedVersions.Count - 1)
                {
                    stringBuilder.Append(',');
                }
            }
            return stringBuilder.ToString();
        }
    }


    public bool official;

    /// <summary>
    /// Mod的唯一识别符
    /// </summary>
    public string PackageId
    {
        get
        {
            return meta.packageId;
        }
    }
    public string Name
    {
        get
        {
            return meta.name;
        }
    }

    public string FolderName;

    public Texture2D previewImage;

    public ModMetaData(DirectoryInfo dir, bool official = false)
    {
        rootDirInt = dir;
        FolderName = dir.Name;
        this.official = official;
        Init();
    }


    //Todo这里之后还要有一个基于steam商店直接下载的版本
    private void Init()//
    {
        meta = DirectXmlLoader.ItemFromXmlFile<ModMetaFileData>(Path.Combine(RootDir.FullName, "About.xml"));//

        string previewPath = Path.Combine(RootDir.FullName, "Preview.png");
        previewImage = new Texture2D(0, 0);
        if (File.Exists(previewPath))
        {
            previewImage.LoadImage(File.ReadAllBytes(previewPath));
        }
    }

}

